/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.resrev.web.page.component;

import com.rafkind.resrev.dao.UserDao;
import com.rafkind.resrev.data.User;
import com.rafkind.resrev.web.page.EditUser;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Fictional Hero
 */
public class EditOrDeleteUserPanel extends Panel {

  @SpringBean
  private UserDao userDao;
  
  public EditOrDeleteUserPanel(String id, IModel<User> model) {
    super(id, model);
    
    Link editLink = new Link("editLink"){
      @Override
      public void onClick() {
        setResponsePage(
            new EditUser(
              new Model(
                (User) EditOrDeleteUserPanel.this.getDefaultModelObject()), 
                getPage()));
      }      
    };
    Link deleteLink = new Link("deleteLink"){
      @Override
      public void onClick() {
        userDao.deleteUser((User) EditOrDeleteUserPanel.this.getDefaultModelObject());
      }      
    };
    deleteLink.add(
        new AttributeAppender(
          "onclick",
          new Model("return confirm('Really delete user " + model.getObject().getLoginName() + "?');"),
          ";"));
    
    add(editLink);
    add(deleteLink);
  }  
}